<?php 
class MyRoute extends CakeRoute {

/**
 * Override the parsing function to find an id based on a slug
 *
 * @param string $url Url string
 * @return boolean
 */
    public function parse($url) {
    	//home route
    	
    	if($url=='/' || $url=='')
    		return array('controller'=>'homes','action'=>'index','named'=>array(),'pass'=>array());
    	//parse url
    	//admin route
    	if($url=='/'.Configure::read('Routing.prefixes.0') || substr($url, 0,7)=='/'.Configure::read('Routing.prefixes.0').'/' || substr($url, 0,7)=='/'.Configure::read('Routing.prefixes.0').'#'  )return false;
		//parse url
		$url=split('\/',$url);		
		$searchurl='/'.$url[1];
		unset($url[0]);
		unset($url[1]);
    	$friendlyUrls=Cache::read('friendlyUrl');
    	if(empty($friendlyUrls)){
    		//try update cache
    		App::uses('CommonComponent', 'Controller/Component');
    		$Common=& new CommonComponent(new ComponentCollection());
    		$Common->updateCacheRoute();
    		$friendlyUrls=Cache::read('friendlyUrl');
    	}
    	if(!empty($friendlyUrls)){
	    	if(array_key_exists($searchurl, $friendlyUrls))
	    	{
	    		
	    		//parse realurl
	    		$realUrl=$friendlyUrls[$searchurl];
				
	    		
	    		$realUrl=split('\/',$realUrl);
				if(!isset($realUrl[1]) || empty($realUrl[1]))$realUrl[1]='homes';
				if(!isset($realUrl[2]) || empty($realUrl[2]))$realUrl[2]='index';
	    		$params=array('controller'=>$realUrl[1],'action'=>$realUrl[2],'named'=>array());
	    		unset($realUrl[0]);
	    		unset($realUrl[1]);
	    		unset($realUrl[2]);
	    		//merge pass param from url and real url
	    		$params['pass']=array_merge($realUrl,$url);
				return $params;
	    	}
	    	else if($redirectUrl=array_search($searchurl, $friendlyUrls)){
	    		header( 'Location: '.SITE_URL.$redirectUrl ) ;
	    	}
    	}
    	
    	//App::uses('CommonComponent', 'Controller/Component');
    	//$Common=& new CommonComponent(new ComponentCollection());
    	
    	//if(substr($url,strlen($url)-5)=='/test')
    	//t(Cache::read('friendlyUrl'));
    	//default param
    	//header( 'Location: '.SITE_URL) ;
    	
    	//return $params;
    	if(Configure::read('debug')!=0)
    		return false;
    	else 
    		trigger_error("invalid url");
		header( 'Location: '.SITE_URL.$redirectUrl ) ;
    	
    }
    
    
    
    
    
    
    
    
}
?>
